package softtech.apps.callblocker;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import softtech.apps.utility.ApplicationConstants;
import softtech.apps.utility.ControlPhoneAction;
import softtech.apps.utility.FileUtility;
import softtech.apps.utility.MyDialog;
import softtech.apps.utility.ViewAlertDialogManager;
import softtech.apps.callblocker.adapter.AdapterPhoneHeader;
import softtech.apps.callblocker.adapter.AdapterSettingDashboard;
import softtech.apps.callblocker.adapter.AdapterSmsSettingContent;
import softtech.apps.callblocker.adapter.AdapterSmsSettingHeader;
import softtech.apps.callblocker.model.ContentSmsBlock;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.HeaderPhone;
import softtech.apps.callblocker.model.Setting;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class ActivitySettings extends Activity {

	final String TAG = "Activity Settings";

	Context mContext;

	DatabaseHanler db;
	Setting setting;

	/**
	 * Init constant
	 */
	// page index
	final int PAGE_HOME = 0;
	final int PAGE_PASSWORD = 1;
	final int PAGE_CALL = 2;
	final int PAGE_SMS = 3;
	final int PAGE_FAKE_ICON = 4;
	final int PAGE_FAKE_PASSWORD = 5;

	/**
	 * dialog alert and notice
	 */
	ViewAlertDialogManager dialogAlert;

	/**
	 * Main fliper use to change page / layout
	 */
	ViewFlipper vflipMain;

	/**
	 * Animation of vflipMain
	 */
	Animation slide_in_left, slide_out_right;

	/**
	 * Selected position of vflipMain
	 */
	int currentPage;

	/**
	 * Page Setting dashboard
	 */
	ListView lvSettingHomeManager;
	AdapterSettingDashboard adapterDashboard;

	/**
	 * Page Setting password
	 */
	EditText edtNewPassword, edtOldPassword, edtConfirmPassword;
	ImageButton ibtnSavePassword;

	/**
	 * Page Setting old password
	 */
	EditText edtNewFakePassword, edtApplicationPassword;
	ImageButton ibtnSaveFakePassword;
	/**
	 * Page Setting SMS
	 */
	ViewFlipper vfSmsContent;
	Button btSmsContentBlockDefault, btSmsContentAdvancedblock,
			btSmsContentHeader;
	Button btAddSmsContent, btBackSmsContent;
	EditText etSmscontent;
	ListView lvsmsContentBlock;
	AdapterSmsSettingContent adapterSmsSettingContent;
	LayoutInflater inf;
	ArrayList<ContentSmsBlock> mListsmsContent;
	/**
	 * Page setting SMS ViewFlipper 1
	 */
	ImageButton btSmsSettingRadio1, btSmsSettingRadio2, btSmsSettingRadio3;
	ImageButton btSmsSettingCheckbox1,btSmsSettingCheckbox2,btSmsSettingCheckbox3;
	RelativeLayout ll1,ll2,ll3,ll4,ll5,ll6;
	/**
	 * Page setting SMS ViewFlipper 3
	 */
	Button btSmsHeaderAdd;
	EditText etSmsHeaderAdd;
	ListView lvSmsHeader;
	ArrayList<HeaderPhone> mListHeaderPhone = new ArrayList<HeaderPhone>();
	AdapterSmsSettingHeader adapterSmsSettingHeader;
	/**
	 * Page Setting call
	 */
	Button btnDefaultCallSetting, btnAdvancedCallSetting;
	ViewFlipper vflCallSetting;
	RadioGroup rgrChooseDefault;
	CheckBox rbtMethodCall1 , rbtMethodCall2,rbtMethodCall4,rbtMethodCall5 ;
	ListView lvHeaderPhone;
	EditText edtSearchHeaderPhone;
	Button btnAddNewNumberCallAdvanced;
	ArrayList<HeaderPhone> listHeaderPhone;
	AdapterPhoneHeader adapterPhoneHeader;
	MyDialog myDialog;

	/**
	 * Page Fake app name + icon
	 */
	ViewFlipper vflFakeApp;
	EditText edtFakeName;
	ImageView imgApplyFakeName;
	Button btnFakeAppName, btnFakeAppIcon;
	ImageView imgSaveFakeIcon;
	RadioGroup rgrFakeIcon1 , rgrFakeIcon2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		mContext = this;
		getActionBar().setDisplayHomeAsUpEnabled(true);
		setId();
		init();
		setFunction();
		
	}

	/**
	 * set id of any view in activity
	 */
	void setId() {
		vflipMain = (ViewFlipper) findViewById(R.id.vflipMain);

		//
		// setting dashboard
		//
		lvSettingHomeManager = (ListView) findViewById(R.id.lvSettingHomeManager);

		//
		// setting password
		//
		edtNewPassword = (EditText) findViewById(R.id.edtNewPassword);
		edtOldPassword = (EditText) findViewById(R.id.edtOldPassword);
		edtConfirmPassword = (EditText) findViewById(R.id.edtConfirmPassword);
		ibtnSavePassword = (ImageButton) findViewById(R.id.ibtnSavePassword);

		// setting fake password
		edtNewFakePassword = (EditText) findViewById(R.id.edtNewFakePassword);
		edtApplicationPassword = (EditText) findViewById(R.id.edtApplicationPassword);
		ibtnSaveFakePassword = (ImageButton) findViewById(R.id.ibtnSaveFakePassword);

		//
		// setting call
		//
		btnDefaultCallSetting = (Button) findViewById(R.id.btnDefaultCallSetting);
		btnAdvancedCallSetting = (Button) findViewById(R.id.btnAdvancedCallSetting);
		vflCallSetting = (ViewFlipper) findViewById(R.id.vflCallSetting);
		rgrChooseDefault = (RadioGroup) findViewById(R.id.rgrChooseDefault);
		lvHeaderPhone = (ListView) findViewById(R.id.lvHeaderPhone);
		edtSearchHeaderPhone = (EditText) findViewById(R.id.edtSearchHeaderPhone);
		btnAddNewNumberCallAdvanced = (Button) findViewById(R.id.btnAddNewNumberCallAdvanced);
		rbtMethodCall1 = (CheckBox) findViewById(R.id.rbtMethodCall1); 
		rbtMethodCall2 = (CheckBox) findViewById(R.id.rbtMethodCall2);
		rbtMethodCall4 = (CheckBox) findViewById(R.id.rbtMethodCall4); 
		rbtMethodCall5 = (CheckBox) findViewById(R.id.rbtMethodCall5); 
		
		//
		// setting fake icon
		//
		vflFakeApp = (ViewFlipper) findViewById(R.id.vflFakeApp);
		edtFakeName = (EditText) findViewById(R.id.edtFakeName);
		imgApplyFakeName = (ImageView) findViewById(R.id.imgApplyFakeName);
		btnFakeAppIcon = (Button) findViewById(R.id.btnFakeAppIcon);
		btnFakeAppName = (Button) findViewById(R.id.btnFakeAppName);
		imgSaveFakeIcon = (ImageView) findViewById(R.id.imgSaveFakeIcon);
		rgrFakeIcon2 = (RadioGroup) findViewById(R.id.rgrFakeIcon2);
		rgrFakeIcon1 = (RadioGroup) findViewById(R.id.rgrFakeIcon1);
	}

	/**
	 * set function for any view in activity
	 */
	void setFunction() {
		lvSettingHomeManager.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				itemSettingClick(position);

			}
		});
	}

	/**
	 * set function for password setting page
	 */
	void setFunctionForPasswordPage() {

		TextWatcher watcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				Boolean isFalse = true;
				isFalse = isFalse
						&& (edtNewPassword.getText().toString().trim().length() == 4);
				isFalse = isFalse
						&& (edtConfirmPassword.getText().toString().trim()
								.length() == 4);
				isFalse = isFalse
						&& (edtConfirmPassword.getText().toString().trim()
								.contentEquals(edtNewPassword.getText()
										.toString().trim()));

				if (!isFalse) {
					edtNewPassword.setCompoundDrawablesWithIntrinsicBounds(
							null, null,
							getResources().getDrawable(R.drawable.ic_text_error),
							null);
					edtConfirmPassword.setCompoundDrawablesWithIntrinsicBounds(
							null, null,
							getResources().getDrawable(R.drawable.ic_text_error),
							null);
				} else {
					edtNewPassword.setCompoundDrawablesWithIntrinsicBounds(
							null, null,
							getResources().getDrawable(R.drawable.ic_enable),
							null);
					edtConfirmPassword.setCompoundDrawablesWithIntrinsicBounds(
							null, null,
							getResources().getDrawable(R.drawable.ic_enable),
							null);
				}
			}
		};

		edtNewPassword.addTextChangedListener(watcher);
		edtConfirmPassword.addTextChangedListener(watcher);

		ibtnSavePassword.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// check input

				String old_pass = setting.getPassCode();

				Boolean isFalse = true;
				isFalse = isFalse
						&& (edtNewPassword.getText().toString().trim().length() == 4);
				isFalse = isFalse
						&& (edtConfirmPassword.getText().toString().trim()
								.length() == 4);
				isFalse = isFalse
						&& (edtConfirmPassword.getText().toString().trim()
								.contentEquals(edtNewPassword.getText()
										.toString().trim()));
				isFalse = isFalse
						&& (edtOldPassword.getText().toString().trim()
								.contentEquals(old_pass));

				if (!isFalse) {
					// change pass failed => notice to user

					return;
				}

				// save data
				setting.setPassCode(edtNewPassword.getText().toString().trim());
				updateSetting();

				// notificate success & go to page home
				selectPage(PAGE_HOME);
			}
		});
	}

	/**
	 * set function for FakePassword setting page
	 */
	void setFunctionForFakePasswordPage() {

		ibtnSaveFakePassword.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				// check input

				String old_pass = setting.getPassCode();

				Boolean isFalse = true;
				isFalse = isFalse
						&& (edtNewFakePassword.getText().toString().trim()
								.length() == 4);
				isFalse = isFalse && (
						edtApplicationPassword.getText().toString().trim().contentEquals(old_pass)
						);
				if (!isFalse) {
					// change pass failed => notice to user

					return;
				}

				// save data
				setting.setPassFakeCode(edtNewFakePassword.getText().toString()
						.trim());
				updateSetting();

				// notificate success & go to page home
				selectPage(PAGE_HOME);
			}
		});
	}

	/**
	 * init value , view of activity
	 */
	void init() {
		// init dialog alert
		dialogAlert = new ViewAlertDialogManager();

		// init setting
		initSetting();

		// init view flipper
		initForFlipMain();

		// init dashboard
		initDashboard();
	}

	/**
	 * init variable setting
	 */
	void initSetting() {
		if (db == null || db.equals(null)) {
			db = new DatabaseHanler(mContext);
		}

		setting = db.getSetting(null);

		// create setting if not have in database
		if (setting == null || setting.equals(null)) {
			// create setting & insert to database
			setting = new Setting("", "", 17, 1, 1);
			db.insertSetting(setting);
			setting = db.getSetting(null);
		}
	}

	/**
	 * init dashboard variable & layout
	 */
	void initDashboard() {
		adapterDashboard = new AdapterSettingDashboard(mContext);
		lvSettingHomeManager.setAdapter(adapterDashboard);
	}

	/**
	 * init page password
	 */
	void initPagePassword() {
		edtOldPassword.setText("");
		edtNewPassword.setText("");
		edtConfirmPassword.setText("");
	}

	/**
	 * init page fake password
	 */
	void initPageFakePassword() {
		
		edtNewFakePassword.setText("");
		edtApplicationPassword.setText("");
	}

	/**
	 * init page sms setting
	 */

	void initPageSmsSetting() {

		initIDSMS();

		inf = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		DatabaseHanler db = new DatabaseHanler(mContext);
		//
		// Block Advanced
		//
		etSmscontent.setText("");
		if (db.getContentSmsBlocks(null, null, null, null, null) != null) {
			mListsmsContent = (ArrayList<ContentSmsBlock>) db
					.getContentSmsBlocks(null, null, null, null, null);
		}
		adapterSmsSettingContent = new AdapterSmsSettingContent(
				mListsmsContent, mContext);
		lvsmsContentBlock.setAdapter(adapterSmsSettingContent);
		//
		// Block header
		//
		etSmsHeaderAdd.setText("");
		if (db.getHeaderPhones("headerPhoneSms", null, null, null, null, null) != null) {
			mListHeaderPhone = (ArrayList<HeaderPhone>) db.getHeaderPhones(
					"headerPhoneSms", null, null, null, null, null);
		}
		adapterSmsSettingHeader = new AdapterSmsSettingHeader(mListHeaderPhone,
				inf, mContext);
		lvSmsHeader.setAdapter(adapterSmsSettingHeader);
	}

	/**
	 * init id sms
	 */
	private void initIDSMS() {
		//
		// setting sms content ViewFlipper2
		//
		btSmsContentBlockDefault = (Button) findViewById(R.id.btsmsContentBlockdefault);
		btSmsContentAdvancedblock = (Button) findViewById(R.id.btsmsContentAdvancedBlock);
		btSmsContentHeader = (Button) findViewById(R.id.btsmsContentBlockheader);
		btAddSmsContent = (Button) findViewById(R.id.btAddSmsContent);
		lvsmsContentBlock = (ListView) findViewById(R.id.lvsmsSettingContent);
		etSmscontent = (EditText) findViewById(R.id.etsmsContentsetting);
		vfSmsContent = (ViewFlipper) findViewById(R.id.vfSmsSettingContent);
		//
		// setting sms default ViewFlipper1
		//
		btSmsSettingRadio1 = (ImageButton) findViewById(R.id.btsmssetting_radio1);
		btSmsSettingRadio2 = (ImageButton) findViewById(R.id.btsmssetting_radio2);
		btSmsSettingRadio3 = (ImageButton) findViewById(R.id.btsmssetting_radio3);
		
		btSmsSettingCheckbox1 = (ImageButton) findViewById(R.id.btsmssetting_checkbox1);
		btSmsSettingCheckbox2 = (ImageButton) findViewById(R.id.btsmssetting_checkbox2);
		btSmsSettingCheckbox3 = (ImageButton) findViewById(R.id.btsmssetting_checkbox3);
		
		ll1 = (RelativeLayout) findViewById(R.id.layout1);
		ll2 = (RelativeLayout)findViewById(R.id.layout3);
		ll3 = (RelativeLayout) findViewById(R.id.layout5);
		
		ll4 = (RelativeLayout) findViewById(R.id.layout8);
		ll5 = (RelativeLayout) findViewById(R.id.layout10);
		ll6 = (RelativeLayout) findViewById(R.id.layout12);
		//
		// setting sms Header ViewFlipper3
		//
		btSmsHeaderAdd = (Button) findViewById(R.id.btAddSmsHeader);
		etSmsHeaderAdd = (EditText) findViewById(R.id.etsmssettingHeader);
		lvSmsHeader = (ListView) findViewById(R.id.lvsmsSettingHeader);
	}

	/**
	 * press tab button sms setting
	 */
	int pressBlockDefault = 1;
	/**
	 * press tab button sms setting
	 */
	int pressBlockAdvanced = 0;
	/**
	 * press tab button sms setting
	 */
	int pressBlockHeader = 0;

	/**
	 * set function for page sms setting
	 */

	void setFunctionForPageSmsSetting() {

		fSmsContentViewFlipper1();

		//
		// View flipper block default
		//
		btSmsContentBlockDefault.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// view block default
				fSmsContentViewBlockDefault();
				if (pressBlockAdvanced == 1) {
					vfSmsContent.setInAnimation(mContext, R.anim.in_from_right);
					vfSmsContent.showPrevious();
				} else if (pressBlockHeader == 1) {
					vfSmsContent.setInAnimation(mContext, R.anim.in_from_right);
					vfSmsContent.showNext();
				}
				fSmsContentViewFlipper1();
				pressBlockDefault = 1;
				pressBlockAdvanced = 0;
				pressBlockHeader = 0;
			}
		});
		//
		// View flipper block advanced
		//

		btSmsContentAdvancedblock
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						fSmsContentViewBlockAdvanced();
						if (pressBlockDefault == 1) {
							vfSmsContent.setInAnimation(mContext,
									R.anim.in_from_left);
							vfSmsContent.showNext();
						} else if (pressBlockHeader == 1) {
							vfSmsContent.setInAnimation(mContext,
									R.anim.in_from_right);
							vfSmsContent.showPrevious();
						}
						fSmsContentViewFlipper2();
						pressBlockAdvanced = 1;
						pressBlockDefault = 0;
						pressBlockHeader = 0;
					}
				});
		//
		// View flipper block header number
		//
		btSmsContentHeader.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				fSmsContentViewBlockHeader();
				if (pressBlockAdvanced == 1) {
					vfSmsContent.setInAnimation(mContext, R.anim.in_from_left);
					vfSmsContent.showNext();
				} else if (pressBlockDefault == 1) {
					vfSmsContent.setInAnimation(mContext, R.anim.in_from_left);
					vfSmsContent.showPrevious();
				}
				fSmsHeaderViewFlipper3();
				pressBlockAdvanced = 0;
				pressBlockDefault = 0;
				pressBlockHeader = 1;
			}
		});

	}

	/**
	 * View block default sms setting
	 */
	private void fSmsContentViewBlockDefault() {
		Drawable dBlockDefault = getResources().getDrawable(
				R.drawable.smssetting_press);
		Drawable dBlockAdvanced = getResources().getDrawable(
				R.drawable.smssetting);
		Drawable dBlockHeader = getResources().getDrawable(
				R.drawable.smssetting);
		// set block default
		btSmsContentBlockDefault.setBackgroundResource(R.drawable.frame_press);
		btSmsContentBlockDefault.setTextColor(Color.parseColor("#383a46"));
		btSmsContentBlockDefault.setEnabled(false);
		btSmsContentBlockDefault.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockDefault, null, null);
		// set block advanced
		btSmsContentAdvancedblock.setBackgroundResource(R.drawable.frame);
		btSmsContentAdvancedblock.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentAdvancedblock.setEnabled(true);
		btSmsContentAdvancedblock.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockAdvanced, null, null);

		// set block header
		btSmsContentHeader.setBackgroundResource(R.drawable.frame);
		btSmsContentHeader.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentHeader.setEnabled(true);
		btSmsContentHeader.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockHeader, null, null);

	}

	/**
	 * View block advanced sms setting
	 */
	private void fSmsContentViewBlockAdvanced() {
		Drawable dBlockDefault = getResources().getDrawable(
				R.drawable.smssetting);
		Drawable dBlockAdvanced = getResources().getDrawable(
				R.drawable.smssetting_press);
		Drawable dBlockHeader = getResources().getDrawable(
				R.drawable.smssetting);
		// set block default
		btSmsContentBlockDefault.setBackgroundResource(R.drawable.frame);
		btSmsContentBlockDefault.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentBlockDefault.setEnabled(true);
		btSmsContentBlockDefault.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockDefault, null, null);
		// set block advanced
		btSmsContentAdvancedblock.setBackgroundResource(R.drawable.frame_press);
		btSmsContentAdvancedblock.setTextColor(Color.parseColor("#383a46"));
		btSmsContentAdvancedblock.setEnabled(false);
		btSmsContentAdvancedblock.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockAdvanced, null, null);

		// set block header
		btSmsContentHeader.setBackgroundResource(R.drawable.frame);
		btSmsContentHeader.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentHeader.setEnabled(true);
		btSmsContentHeader.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockHeader, null, null);
	}

	/**
	 * View block header sms setting
	 */
	private void fSmsContentViewBlockHeader() {
		Drawable dBlockDefault = getResources().getDrawable(
				R.drawable.smssetting);
		Drawable dBlockAdvanced = getResources().getDrawable(
				R.drawable.smssetting);
		Drawable dBlockHeader = getResources().getDrawable(
				R.drawable.smssetting_press);
		// set block default
		btSmsContentBlockDefault.setBackgroundResource(R.drawable.frame);
		btSmsContentBlockDefault.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentBlockDefault.setEnabled(true);
		btSmsContentBlockDefault.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockDefault, null, null);
		// set block advanced
		btSmsContentAdvancedblock.setBackgroundResource(R.drawable.frame);
		btSmsContentAdvancedblock.setTextColor(Color.parseColor("#ffffff"));
		btSmsContentAdvancedblock.setEnabled(true);
		btSmsContentAdvancedblock.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockAdvanced, null, null);

		// set block header
		btSmsContentHeader.setBackgroundResource(R.drawable.frame_press);
		btSmsContentHeader.setTextColor(Color.parseColor("#383a46"));
		btSmsContentHeader.setEnabled(false);
		btSmsContentHeader.setCompoundDrawablesWithIntrinsicBounds(null,
				dBlockHeader, null, null);
	}

	private void checkChooseSmsDefault(){
		DatabaseHanler db = new DatabaseHanler(mContext);
		Setting set = new Setting();
		set = db.getSetting(null);
		switch (set.getDefaultModel()&15) {
		case 1:
			btSmsSettingRadio1
			.setBackgroundResource(R.drawable.smssettingblock_press);
	btSmsSettingRadio2
			.setBackgroundResource(R.drawable.smssettingblock);
	btSmsSettingRadio3
			.setBackgroundResource(R.drawable.smssettingblock);
			break;
		case 2:
			btSmsSettingRadio2
			.setBackgroundResource(R.drawable.smssettingblock_press);
	btSmsSettingRadio1
			.setBackgroundResource(R.drawable.smssettingblock);
	btSmsSettingRadio3
			.setBackgroundResource(R.drawable.smssettingblock);
			break;
		case 3:
			btSmsSettingRadio3
			.setBackgroundResource(R.drawable.smssettingblock_press);
	btSmsSettingRadio2
			.setBackgroundResource(R.drawable.smssettingblock);
	btSmsSettingRadio1
			.setBackgroundResource(R.drawable.smssettingblock);
			break;
		default:
			break;
		}
	}
	private void checkChooseSmsMode(){
		DatabaseHanler db = new DatabaseHanler(mContext);
		Setting set = new Setting();
		set = db.getSetting(null);
		if((set.getBlockSmsSetting()&1)==1){
			btSmsSettingCheckbox1.setBackgroundResource(R.drawable.check);
		}
		if ((set.getBlockSmsSetting() & 2) == 2) {
			btSmsSettingCheckbox2.setBackgroundResource(R.drawable.check);
		}
		if ((set.getBlockSmsSetting() & 4) == 4) {
			btSmsSettingCheckbox3.setBackgroundResource(R.drawable.check);
		}
	}
	/**
	 * View block default sms sestting ViewFlipper1 onclick
	 */
	private void fSmsContentViewFlipper1() {
		
		//check choose sms default
		checkChooseSmsDefault();
		//check choose sms mode
		checkChooseSmsMode();
		
		ll1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|1);
			}
		});
		ll2.setOnClickListener(new View.OnClickListener() {
			// Delete device
			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|2);
			}
		});
		ll3.setOnClickListener(new View.OnClickListener() {
			// delete app
			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|3);
			}
		});
		btSmsSettingRadio1.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|1);
			}
		});
		btSmsSettingRadio2.setOnClickListener(new View.OnClickListener() {
			// Delete device
			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|2);
			}
		});
		btSmsSettingRadio3.setOnClickListener(new View.OnClickListener() {
			// delete app
			@Override
			public void onClick(View v) {
				DatabaseHanler db = new DatabaseHanler(mContext);
				btSmsSettingRadio3
						.setBackgroundResource(R.drawable.smssettingblock_press);
				btSmsSettingRadio2
						.setBackgroundResource(R.drawable.smssettingblock);
				btSmsSettingRadio1
						.setBackgroundResource(R.drawable.smssettingblock);
				Setting item = new Setting();
				item = db.getSetting(null);
				db.updateSettingDefault((item.getDefaultModel()&240)|3);
			}
		});
//choose unknown contact
		ll4.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting itemSetting = new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&1)==1){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^1);
					btSmsSettingCheckbox1.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|1);
					btSmsSettingCheckbox1.setBackgroundResource(R.drawable.check);
				}
			}
		});
		//choose block content
		ll5.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting itemSetting = new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&2)==2){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^2);
					btSmsSettingCheckbox2.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|2);
					btSmsSettingCheckbox2.setBackgroundResource(R.drawable.check);
				}
			}
		});
		//choose block header
		ll6.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting  itemSetting	= new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&4)==4){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^4);
					btSmsSettingCheckbox3.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|4);
					btSmsSettingCheckbox3.setBackgroundResource(R.drawable.check);
				}
			}
		});	
		btSmsSettingCheckbox1.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting itemSetting = new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&1)==1){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^1);
					btSmsSettingCheckbox1.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|1);
					btSmsSettingCheckbox1.setBackgroundResource(R.drawable.check);
				}
			}
		});
		btSmsSettingCheckbox2.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting itemSetting = new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&2)==2){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^2);
					btSmsSettingCheckbox2.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|2);
					btSmsSettingCheckbox2.setBackgroundResource(R.drawable.check);
				}
			}
		});
		//choose block header
		btSmsSettingCheckbox3.setOnClickListener(new View.OnClickListener() {
			DatabaseHanler db = new DatabaseHanler(mContext);
			Setting  itemSetting	= new Setting();
			@Override
			public void onClick(View v) {
				itemSetting = db.getSetting(null);
				if((itemSetting.getBlockSmsSetting()&4)==4){
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()^4);
					btSmsSettingCheckbox3.setBackgroundResource(R.drawable.smssettingblock);
				}
				else{
					db.updateSettingBlockSms(itemSetting.getBlockSmsSetting()|4);
					btSmsSettingCheckbox3.setBackgroundResource(R.drawable.check);
				}
			}
		});	
		
	}

	/**
	 * View Block advanced ViewFlipper2 onclick
	 */
	private void fSmsContentViewFlipper2() {
		final DatabaseHanler db = new DatabaseHanler(mContext);
		btAddSmsContent.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							return;
						}

						if (db.getContentSmsBlocks(
								null,
								ContentSmsBlock.CONTENT + "=?",
								new String[] { myDialog.getKeyCodeString()
										.trim() }, null, null).size() > 0) {
							return;
						}

						db.insertContentSmsBlock(new ContentSmsBlock(myDialog
								.getKeyCodeString().trim()));
						etSmscontent.setText("");
						mListsmsContent = (ArrayList<ContentSmsBlock>) db
								.getContentSmsBlocks(null, null, null, null,
										null);
						adapterSmsSettingContent.update(mListsmsContent);
					}
				};
				myDialog = new MyDialog(mContext, "Add new content",
						"Add new content", "Save", "Cancel", leftClick, null,
						etSmscontent.getText().toString().trim());
				myDialog.showDialog();
			}
		});

		etSmscontent.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {

				String text = etSmscontent.getText().toString();

				if (text.equals("") || text.equals(null)) {
					mListsmsContent = (ArrayList<ContentSmsBlock>) db
							.getContentSmsBlocks(null, null, null, null, null);
				} else {
					mListsmsContent = (ArrayList<ContentSmsBlock>) db
							.getContentSmsBlocks(null, ContentSmsBlock.CONTENT
									+ " LIKE ? ", new String[] { "%" + text
									+ "%" }, null, null);
				}
				adapterSmsSettingContent.update(mListsmsContent);
			}
		});
	}

	/**
	 * View Block Header ViewFlipper3 onclick
	 */
	private void fSmsHeaderViewFlipper3() {
		final DatabaseHanler db = new DatabaseHanler(mContext);
		btSmsHeaderAdd.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							return;
						}

						if (db.getHeaderPhones(
								null,
								HeaderPhone.HEADER + "=?",
								new String[] { myDialog.getKeyCodeString()
										.trim() }, null, null).size() > 0) {
							return;
						}

						db.insertHeaderPhoneSms(new HeaderPhone(myDialog
								.getKeyCodeString().trim()));
						etSmsHeaderAdd.setText("");
						mListHeaderPhone = (ArrayList<HeaderPhone>) db
								.getHeaderPhones(null, null, null, null, null);
						adapterSmsSettingContent.update(mListsmsContent);
					}
				};
				myDialog = new MyDialog(mContext, "Add new phone header",
						"Add new phone header", "Save", "Cancel", leftClick,
						null, etSmsHeaderAdd.getText().toString().trim());
				myDialog.showDialog();
			}
		});
		etSmsHeaderAdd.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				String text = etSmsHeaderAdd.getText().toString();

				if (text.equals("") || text.equals(null)) {
					mListHeaderPhone = (ArrayList<HeaderPhone>) db
							.getHeaderPhones("headerPhoneSms", null, null,
									null, null, null);
				} else {
					mListHeaderPhone = (ArrayList<HeaderPhone>) db
							.getHeaderPhones("headerPhoneSms", null,
									HeaderPhone.HEADER + " LIKE ? ",
									new String[] { "%" + text + "%" }, null,
									null);
				}
				adapterSmsSettingHeader.update(mListHeaderPhone);
			}
		});
	}

	/**
	 * get old fake icon name
	 * 
	 * @return
	 */
	String getOldFakeIconName() {
		// get old face icon name
		FileUtility tsf = new FileUtility();
		String old_name = tsf.LoadContentFile(mContext,
				ApplicationConstants.OLD_FAKE_ICON_NAME);
		if (!FileUtility.checkStringNotNull(old_name)) {
			old_name = getResources().getString(R.string.app_name);
		}

		return old_name;
	}
	
	/**
	 * get old fake icon name
	 * 
	 * @return
	 */
	int getOldFakeIconId() {
		FileUtility tsf = new FileUtility();
		String old_name = tsf.LoadContentFile(mContext,
				ApplicationConstants.OLD_FAKE_ICON_ID);
		int resid;
		if (!FileUtility.checkStringNotNull(old_name)) {
			resid = R.drawable.ic_launcher;
		}else{
			try{
				resid = Integer.valueOf(old_name);
			}catch(Exception e){
				resid = R.drawable.ic_launcher;
			}
		}
		return resid;
	}
	
	/**
	 * create a new fake icon
	 * 
	 * @param new_name
	 *            : new app name
	 * @param resId
	 *            : resource id of new icon
	 */
	void createFakeIcon(String new_name, int resId) {

		// get old face icon name
		FileUtility tsf = new FileUtility();
		String old_name = tsf.LoadContentFile(mContext,
				ApplicationConstants.OLD_FAKE_ICON_NAME);

		// delete old icon
		ControlPhoneAction.deleteShortCut(mContext, getResources()
				.getString(R.string.app_name));
		if (old_name.trim().length() > 0) {
			ControlPhoneAction.deleteShortCut(mContext, old_name);
		} 
		// create new shortcut

		ControlPhoneAction.addShortcut(mContext, new_name, resId);
		tsf.SaveContentFile(mContext, ApplicationConstants.OLD_FAKE_ICON_NAME, new_name);
		
	}

	//
	// menu item selected
	//
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case android.R.id.home:
			if (currentPage == 0) {
				Intent inDasboard = new Intent(ActivitySettings.this,
						ActivityDashboard.class);
				startActivity(inDasboard);
				finish();
			} else {
				selectPage(PAGE_HOME);
			}

			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	/**
	 * init animation, first page for vflipMain
	 */
	void initForFlipMain() {
		slide_in_left = AnimationUtils.loadAnimation(this,
				android.R.anim.slide_in_left);
		slide_out_right = AnimationUtils.loadAnimation(this,
				android.R.anim.slide_out_right);

		vflipMain.setInAnimation(slide_in_left);
		vflipMain.setOutAnimation(slide_out_right);
		currentPage = 0;
	}

	/**
	 * Go to the new page of vflipmain
	 * 
	 * @param page
	 *            : position that you want to transfer
	 */
	void selectPage(int page) {
		if (currentPage == page) {
			return;
		}
		currentPage = page;
		String title_name = "";

		switch (page) {
		case PAGE_HOME:
			break;
		case PAGE_PASSWORD:
			initPagePassword();
			setFunctionForPasswordPage();
			break;
		case PAGE_CALL:
			initPageCallSetting();
			setFunctionForPageCallSetting();
			showPageCallSetting(0);
			break;
		case PAGE_SMS:
			title_name = "SMS Setting";
			initPageSmsSetting();
			setFunctionForPageSmsSetting();
			break;
		case PAGE_FAKE_ICON:
			initPageFakeApp();
			setFunctionForPageFakeIcon();
			break;
		case PAGE_FAKE_PASSWORD:
			initPageFakePassword();
			setFunctionForFakePasswordPage();
			break;
		default:
			break;
		}
		if(title_name.contentEquals("")){
			title_name = getResources().getString(R.string.app_name);
		}
		vflipMain.setDisplayedChild(page);
		this.getActionBar().setTitle(title_name);
	}

	/**
	 * action when choose item setting
	 */
	void itemSettingClick(int position) {
		switch (position) {
		case AdapterSettingDashboard.CASE_PASSWORD: // setting password for app
			selectPage(PAGE_PASSWORD);
			break;
		case AdapterSettingDashboard.CASE_SMS_SETTING: // setting block sms
			selectPage(PAGE_SMS);
			break;
		case AdapterSettingDashboard.CASE_CALL_SETTING: // setting block call
			selectPage(PAGE_CALL);
			break;

		case AdapterSettingDashboard.CASE_HISTORY_TIME: // setting time limited
														// of history save
			/**
			 * open dialog historysetting
			 */
			final Dialog dialog = new Dialog(mContext, R.style.cust_dialog);
			dialog.setContentView(R.layout.dialog_history_setting);
			dialog.setTitle(mContext.getResources().getString(R.string.title_dialog_history_setting));
			
			
			final RadioGroup rgHistory = (RadioGroup) dialog.findViewById(R.id.rgHistory);
			RadioButton r1 = (RadioButton) dialog.findViewById(R.id.rbOneDay);
			RadioButton r2 = (RadioButton) dialog.findViewById(R.id.rbTwoDay);
			RadioButton r3 = (RadioButton) dialog.findViewById(R.id.rbOneWeek);
			RadioButton r4 = (RadioButton) dialog.findViewById(R.id.rbFifteen);
			RadioButton r5 = (RadioButton) dialog.findViewById(R.id.rbOneMonth);
			
			readDataFromRadioGroup(r1, r2, r3, r4, r5);
			
			Button btnSaveSettingHistory = (Button) dialog.findViewById(R.id.btnSaveSetingHistory);
			btnSaveSettingHistory.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {

					int idRg = rgHistory.getCheckedRadioButtonId();
					writeDataFromRadioGroup(idRg);
					dialog.dismiss();
				}
			});
			Button btnCancelSetingHistory = (Button) dialog.findViewById(R.id.btnCancelSetingHistory);
			btnCancelSetingHistory.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
		case AdapterSettingDashboard.CASE_FAKE_ICON: // fake icon
			selectPage(PAGE_FAKE_ICON);
			break;
		case AdapterSettingDashboard.CASE_FAKE_PASSWORD: // fake password
			selectPage(PAGE_FAKE_PASSWORD);
			break;
		default:
			break;
		}
	}

	protected void writeDataFromRadioGroup(int idRg) {
		
		FileUtility fileUltility = new FileUtility();
		switch (idRg) {
		case R.id.rbOneDay:
			fileUltility.SaveContentFile(mContext, ApplicationConstants.FILE_LIMITED, "1");
			break;
		case R.id.rbTwoDay:
			fileUltility.SaveContentFile(mContext, ApplicationConstants.FILE_LIMITED, "2");
			break;
		case R.id.rbOneWeek:
			fileUltility.SaveContentFile(mContext, ApplicationConstants.FILE_LIMITED, "7");
			break;
		case R.id.rbFifteen:
			fileUltility.SaveContentFile(mContext, ApplicationConstants.FILE_LIMITED, "15");
			break;
		case R.id.rbOneMonth:
			fileUltility.SaveContentFile(mContext, ApplicationConstants.FILE_LIMITED, "30");
			break;
		default:
			break;
		}
	}
	protected void readDataFromRadioGroup(RadioButton r1, RadioButton r2, RadioButton r3, RadioButton r4,
			RadioButton r5){
		FileUtility fileUltility = new FileUtility();
		String text = fileUltility.LoadContentFile(mContext, ApplicationConstants.FILE_LIMITED);
		int day = Integer.parseInt(text);
		switch (day) {
		case 1:
			r1.setChecked(true);
			break;
		case 2:
			r2.setChecked(true);
			break;
		case 7:
			r3.setChecked(true);
			break;
		case 15:
			r4.setChecked(true);
			break;
		case 30:
			r5.setChecked(true);
			break;
		default:
			break;
		}
	}

	/**
	 * update setting
	 */
	void updateSetting() {
		if (setting == null) {
			initSetting();
		}

		db.updateSetting(setting);

	}

	/**
	 * init page call setting
	 */
	void initPageCallSetting() {

		vflCallSetting.setInAnimation(slide_in_left);
		vflCallSetting.setOutAnimation(slide_out_right);
	}

	/**
	 * Init page call setting advanced
	 */
	void initPageCallSettingAdvanced(String search) {
		listHeaderPhone = db.getHeaderPhones(null, HeaderPhone.HEADER
				+ " LIKE '%" + search + "%'", null, null, null);
		adapterPhoneHeader = new AdapterPhoneHeader(mContext, listHeaderPhone);
		lvHeaderPhone.setAdapter(adapterPhoneHeader);
		lvHeaderPhone.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					final int position, long id) {
				if (listHeaderPhone == null || listHeaderPhone.equals(null)
						|| listHeaderPhone.size() == 0) {
					return;
				}
				final HeaderPhone item = (HeaderPhone) adapterPhoneHeader
						.getItem(position);
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							Toast.makeText(mContext, "cannot save",
									Toast.LENGTH_SHORT).show();
							return;
						}

						if (db.getHeaderPhones(
								null,
								HeaderPhone.HEADER + "=?",
								new String[] { myDialog.getKeyCodeString()
										.trim() }, null, null).size() > 0) {
							Toast.makeText(mContext, "cannot save",
									Toast.LENGTH_SHORT).show();
							return;
						}
						item.setHeader(myDialog.getKeyCodeString().trim());
						db.updateHeaderPhoneCall(item,
								HeaderPhone.ID + " = ? ", new String[] { ""
										+ item.getId() });

						edtSearchHeaderPhone.setText("");
						Toast.makeText(mContext, "save success! ",
								Toast.LENGTH_SHORT).show();
					}
				};
				myDialog = new MyDialog(mContext, "Edit profile",
						"Edit profile", "Save", "Cancel", leftClick, null, item
								.getHeader());
				myDialog.showDialog();
			}
		});
	}

	/**
	 * 
	 * @param page
	 */
	@SuppressWarnings("deprecation")
	void showPageCallSetting(int page) {
		switch (page) {
		case 0:
			// change tab color
			int current_mode = (setting.getDefaultModel() >> 4) - 1;
			((RadioButton) rgrChooseDefault.getChildAt(current_mode))
					.setChecked(true);
			btnDefaultCallSetting.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_header_phone, 0, 0);
			btnAdvancedCallSetting.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call_default, 0, 0);
			btnDefaultCallSetting.setBackgroundDrawable(getResources()
					.getDrawable(R.drawable.frame_press));
			btnAdvancedCallSetting.setBackgroundDrawable(getResources()
					.getDrawable(R.drawable.frame));
			
			int block_call = setting.getBlockCallSetting();
			
			rbtMethodCall1.setChecked((block_call&1) != 0 );
			rbtMethodCall2.setChecked((block_call & 2) != 0);
			rbtMethodCall4.setChecked((block_call & 4) != 0);
			rbtMethodCall5.setChecked((block_call & 8) != 0);
			
			break;
		case 1:
			// change tab color
			btnDefaultCallSetting.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_call_default, 0, 0);
			btnAdvancedCallSetting.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_tab_header_phone, 0, 0);
			btnDefaultCallSetting.setBackgroundDrawable(getResources()
					.getDrawable(R.drawable.frame));
			btnAdvancedCallSetting.setBackgroundDrawable(getResources()
					.getDrawable(R.drawable.frame_press));

			//
			// init list view
			//
			initPageCallSettingAdvanced("");
			break;

		default:
			break;
		}
		vflCallSetting.setDisplayedChild(page);
	}

	/**
	 * Set function for page call setting
	 */
	void setFunctionForPageCallSetting() {
		btnDefaultCallSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPageCallSetting(0);
			}
		});

		btnAdvancedCallSetting.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPageCallSetting(1);
			}
		});

		rgrChooseDefault
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						int index = group.indexOfChild(findViewById(group
								.getCheckedRadioButtonId()));
						int value_insert = index + 1;
						value_insert = value_insert << 4;
						value_insert = value_insert
								| (setting.getDefaultModel() & 15);
						setting.setDefaultModel(value_insert);
						updateSetting();
					}
				});

		btnAddNewNumberCallAdvanced.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				OnClickListener leftClick = new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (myDialog.getKeyCodeString().length() == 0) {
							return;
						}

						if (db.getHeaderPhones(
								null,
								HeaderPhone.HEADER + "=?",
								new String[] { myDialog.getKeyCodeString()
										.trim() }, null, null).size() > 0) {
							return;
						}

						db.insertHeaderPhoneCall(new HeaderPhone(myDialog
								.getKeyCodeString().trim()));
						edtSearchHeaderPhone.setText("");
					}
				};
				myDialog = new MyDialog(mContext, "Add new phone header",
						"Add new phone header", "Save", "Cancel", leftClick,
						null, edtSearchHeaderPhone.getText().toString().trim());
				myDialog.txtCheckCodeSetInputType(InputType.TYPE_CLASS_PHONE);
				myDialog.txtCheckCodeSetHint(getResources().getString(R.string.hint_new_phone_header));
				myDialog.showDialog();

			}
		});

		edtSearchHeaderPhone.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

				initPageCallSettingAdvanced(s.toString().trim());
			}
		});

		rbtMethodCall1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Boolean isCheck = rbtMethodCall1.isChecked();
				
				if(isCheck){
					// block unknow
					setting.setBlockCallSetting(setting.getBlockCallSetting()|1);
					updateSetting();
				}else{
					// khong block unknown
					setting.setBlockCallSetting(setting.getBlockCallSetting()^1);
					updateSetting();
					
				}
				
			}
		});
		rbtMethodCall2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Boolean isCheck = rbtMethodCall2.isChecked();
				
				if(isCheck){
					// cho hien widget
					setting.setBlockCallSetting(setting.getBlockCallSetting()|2);
					updateSetting();
				}else{
					// khong cho hien widget
					setting.setBlockCallSetting(setting.getBlockCallSetting()^2);
					updateSetting();
					
				}
			}
		});
		rbtMethodCall4.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Boolean isCheck = rbtMethodCall4.isChecked();
				
				if (isCheck) {
					// block header
					setting.setBlockCallSetting(setting.getBlockCallSetting()|4);
					updateSetting();					
				} else {
					// none block header
					setting.setBlockCallSetting(setting.getBlockCallSetting()^4);
					updateSetting();
					
				}
			}
		});
		rbtMethodCall5.setOnClickListener(new OnClickListener() {
			DatabaseHanler db= new DatabaseHanler(mContext);
			@Override
			public void onClick(View v) {
				Boolean isCheck = rbtMethodCall5.isChecked();
				if (isCheck) {
					// show notification
					setting.setBlockCallSetting(setting.getBlockCallSetting()|8);
					updateSetting();
				} else {
					// none show notification
					setting.setBlockCallSetting(setting.getBlockCallSetting()^8);
					updateSetting();
				}
			}
		});
		
	}

	/**
	 * init page fake app
	 */
	void initPageFakeApp() {
		vflFakeApp.setInAnimation(slide_in_left);
		vflFakeApp.setOutAnimation(slide_out_right);
		edtFakeName.setText(getOldFakeIconName());
	}

	/**
	 * show page fake app
	 * 
	 * @param page
	 *            0 - page fake app name 
	 *            1 - page fake icon app
	 */
	void showPageFakeApp(int page) {
		switch (page) {
		case 0:
			// change tab color

			btnFakeAppName.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.frame_press));
			btnFakeAppIcon.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.frame));
			break;
		case 1:
			// change tab color
			btnFakeAppName.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.frame));
			btnFakeAppIcon.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.frame_press));
		default:
			break;
		}

		vflFakeApp.setDisplayedChild(page);
		// setFunctionForPageFakeIcon(page);
	}

	/**
	 * set function for page fake icon
	 */
	void setFunctionForPageFakeIcon() {
		btnFakeAppName.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showPageFakeApp(0);
			}
		});

		btnFakeAppIcon.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showPageFakeApp(1);
			}
		});
		
		imgApplyFakeName.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				createFakeIcon(edtFakeName.getText().toString().trim(),getOldFakeIconId() );
			}
		});
		
		rgrFakeIcon1.setOnCheckedChangeListener(checkFake1);
		
		rgrFakeIcon2.setOnCheckedChangeListener(checkFake2);
		
		imgSaveFakeIcon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int resId = getDrawAbleId(resIdFakeIcon);
				if(resId == 0){
					createFakeIcon(getOldFakeIconName(),getOldFakeIconId() );
				}else{
					createFakeIcon(getOldFakeIconName(),resId );
				}
				
			}
		});
	}
	
	int resIdFakeIcon = 0;
	
	OnCheckedChangeListener checkFake1 = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			rgrFakeIcon2.setOnCheckedChangeListener(null);
			rgrFakeIcon2.clearCheck();
			rgrFakeIcon2.setOnCheckedChangeListener(checkFake2);
			resIdFakeIcon = checkedId;
		}
	};
	
	OnCheckedChangeListener checkFake2 = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			rgrFakeIcon1.setOnCheckedChangeListener(null);
			rgrFakeIcon1.clearCheck();
			rgrFakeIcon1.setOnCheckedChangeListener(checkFake1);
			resIdFakeIcon = checkedId;
		}
	};
	
	int getDrawAbleId(int resId){
		switch (resId) {
		case R.id.rbtFakeIcon0:
			return R.drawable.fakeicon1;
		case R.id.rbtFakeIcon1:
			return R.drawable.fakeicon2;
		case R.id.rbtFakeIcon2:
			return R.drawable.fakeicon3;
		case R.id.rbtFakeIcon3:
			return R.drawable.fakeicon4;
		case R.id.rbtFakeIcon4:
			return R.drawable.fakeicon5;
		case R.id.rbtFakeIcon5:
			return R.drawable.fakeicon6;
		case R.id.rbtFakeIcon6:
			return R.drawable.fakeicon7;
		case R.id.rbtFakeIcon7:
			return R.drawable.fakeicon8;

		default:
			return 0;
		}
	}
	/**
	 * back overide
	 */
	@Override
	public void onBackPressed() {
		
		super.onBackPressed();
		Intent inDash = new Intent(ActivitySettings.this,ActivityDashboard.class);
		startActivity(inDash);
		finish();
	}
	
}
